<?php
namespace Admin\Controller;
use Think\Controller;
class FormsController extends CommonController {
	//历史订单页面
    public function lsindex(){
    	//获取数据库
        $Forms = M('mvp_forms');
        //获取关键字
        if(!empty($_GET['keywords'])){
            //建议使用数组形式来创建where条件
            $where['formnumber'] = array('like','%'.$_GET["keywords"].'%');
            // $where['name'] = array('like','%'.$_GET["keywords"].'%');
            // $where['username'] = array('eq','admin');
            // $where['age'] = array('lt',18);
        }else{
            $where="";
        }
        $where['mvp_forms.status'] = 4;
        //获取总数
        $count = $Forms->where($where)->count();
        //获取每页显示的数量
        $num = !empty($_GET['num']) ? $_GET['num'] : 10;
        //实例化分页对象
        $page = new \Think\Page($count,$num);
        //获取limit参数
        $limit = $page->firstRow.','.$page->listRows;
        //获取页码信息(页码字符串)
        $pages = $page->show();
        $Finfo = $Forms->where($where)->limit($limit)->field('mvp_forms.id,mvp_forms.fprice,mvp_forms.ask,mvp_forms.formnumber,mvp_forms.ftime,mvp_forms.status,mvp_user.username')->join('mvp_user on mvp_forms.uid=mvp_user.id')->order('mvp_forms.id')->select();
        // var_dump($Forms);
        // echo $Forms->_sql();die;
        $this->assign('Finfo',$Finfo);
        $this->assign('pages',$pages);
        $this->display();
    }
    //新订单列表页面
    public function index(){
        // var_dump($_GET['num']);
    	//获取数据库
    	$Forms = M('mvp_forms');
        
        
        // var_dump($_GET);die;
        //获取关键字
        if(!empty($_GET['keywords'])){
            //建议使用数组形式来创建where条件
            $where['formnumber'] = array('like','%'.$_GET["keywords"].'%');
            // $where['name'] = array('like','%'.$_GET["keywords"].'%');
            // $where['username'] = array('eq','admin');
            // $where['age'] = array('lt',18);
        }else{
            $where = "";
        }
        $where['mvp_forms.status'] = array('neq','4');
        //获取总数
        $count = $Forms->where($where)->count();
        // var_dump($count);
        //获取每页显示的数量
        $num = !empty($_GET['num']) ? $_GET['num'] : 10;
        //实例化分页对象
        $page = new \Think\Page($count,$num);
        //获取limit参数
        $limit = $page->firstRow.','.$page->listRows;
        // var_dump($limit);
        //获取页码信息(页码字符串)
        $pages = $page->show();
    	//获取数据
    	$info = $Forms->where($where)->limit($limit)->field('mvp_forms.id,mvp_forms.fprice,mvp_forms.ask,mvp_forms.formnumber,mvp_forms.ftime,mvp_forms.status,mvp_user.username')->join('mvp_user on mvp_forms.uid=mvp_user.id')->order('mvp_forms.id')->select();
        // var_dump($pages);
        // echo $Forms->_sql();
        // var_dump($info);die;
        $this->assign('info',$info);
    	$this->assign('pages',$pages);
    	// var_dump($info);
    	$this->display();
    }

    public function edit(){
    	//获取要修改的id
        // var_dump($_GET);
    	$id = I('get.id');
        $username = I('get.username');
        $formnumber = I('get.formnumber');
    	//查询数据
    	$Forms = M('mvp_forms');
        $foods = M('mvp_foods');
        $formfoods = M('mvp_formfoods');
        $info = $Forms->where('id='.$id)->find();
        $info['username'] = $username;
        $where['mvp_formfoods.formfoodsnumber'] = array('eq',$formnumber);
        // $where['mvp_formfoods.formfoodsnumber'] = array('eq','mvp_forms.formnumber');
        // $where['mvp_foods.id'] = array('eq','mvp_formfoods.fid'); 
        $forminfo = $formfoods->where($where)->join('mvp_foods on mvp_foods.id = mvp_formfoods.fid')->select();
        // echo $formfoods->_sql();
        // var_dump($forminfo);
        foreach ($forminfo as $key => $value) {
            // var_dump($key);
            // var_dump($value);
            $infos[$key]['name'] = $forminfo[$key]['name'];
            $infos[$key]['number'] = $forminfo[$key]['number'];
        }
    	
        //查出所有的分类
        // $Mclass = M('mvp_class');
        // $cinfo = $Mclass->select();
    	// var_dump($info);
    	// 分配

        $this->assign('info',$info);
        $this->assign('infos',$infos);
    	// $this->assign('cinfo',$cinfo);
    	//展示要修改的类
    	$this->display();
    }

    public function update(){
    	// var_dump($_POST);die;
    	//查询数据
    	$Forms = M('mvp_forms');
        $Forms->create();
        $num = $Forms->where('id = '.$_POST['id'])->save();
    	// echo $num;
    	// echo $Mclass->_sql();die;
    	if($num == 1){
    		$this->success('修改成功',U('Admin/Forms/index'),3);
    	}else{
    		$this->error('修改失败',U('Admin/Forms/index'),3);

    	}
    }

}